package com.qf.service.impl;

import com.qf.dao.TbFilmPictureRepository;
import com.qf.pojo.entity.TbFilmPicture;
import com.qf.pojo.request.TbFilmPictureReq;
import com.qf.pojo.response.BaseResponse;
import com.qf.service.TbFilmPictureService;
import com.qf.service.TbFirstPictureService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TbFilmPictureServiceImpl implements TbFilmPictureService {
    @Autowired
    TbFilmPictureRepository tbFilmPictureRepository;
    /**
     * 查询所有电影图片
     *
     * @param page 当前页
     * @param size 当前页显示行数
     * @return 响应对象
     */
    public BaseResponse findAll(Integer page, Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);
        Page<TbFilmPicture> all = tbFilmPictureRepository.findAll(of);
        List<TbFilmPicture> tbFilmPictures = all.getContent();
        long total = all.getTotalElements();
        return new BaseResponse(1,"success",tbFilmPictures,total);
    }

    /**
     * 新增或者修改电影图片
     *
     * @param tbFilmPictureReq 电影图片请求对象
     * @return 响应对象
     */
    public BaseResponse updateOrSave(TbFilmPictureReq tbFilmPictureReq) {
        TbFilmPicture tbFilmPicture = new TbFilmPicture();
        BeanUtils.copyProperties(tbFilmPictureReq,tbFilmPicture);
        tbFilmPictureRepository.saveAndFlush(tbFilmPicture);
        return new BaseResponse().SUCCESS();
    }

    /**
     * 删除电影图片
     *
     * @param fid 电影图片id
     * @return 响应对象
     */
    public BaseResponse deleteById(Integer fid) {
        tbFilmPictureRepository.deleteById(fid);
        return new BaseResponse().SUCCESS();
    }
}
